package Concurrent;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description：
 * Author: zhangc
 * Date：2017/8/16 14:32
 */
public class ExecutorService {

    public static void main(String[] args) {
//        java.util.concurrent.ExecutorService executorService = Executors.newFixedThreadPool(5);
//        Future<String> future = executorService.submit(() -> {
//            return "zhang";
//        });
        final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
//        Future<String> future = threadPool.submit(() -> {
//            Thread.sleep(5000);
//            System.out.println("zh====");
//            return "zha===";
//        });

        AtomicInteger count = new AtomicInteger(0);

        Future<Integer>[] futures = new Future[ 10 ];
        for (int i = 0; i < 10; i++) {
            futures[ i ] = threadPool.submit(() -> {
                Thread.sleep(1000);
                return count.getAndIncrement();
            });
        }

        try {
            for (int i = 0; i < futures.length; i++) {
                System.out.println(futures[ i ].get());
            }
            threadPool.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

}
